#pragma  once
#include "..\RealTimeDataSet\RealTimeDataSet.h"
 
#include "base\at_exit.h"
#include "base\message_loop.h"
#include "base\location.h"
#include "base\bind.h"
#include "..\..\Content\ThreadManager.h"

namespace
{
	const static  int kUpdateDataCount = 10000;
	struct ArrayContainer
	{
	public:
		typedef  boost::shared_array<uint16>	IntArray;
		void  Push(IntArray  array)		{	array_list.push_back(array);	}

		uint16  GetAt(int  index)	
		{
			return array_list[index/kUpdateDataCount][index%kUpdateDataCount];
		}
	private:
		std::vector<IntArray>	array_list;
	};
}

class  TestDataGenerator : public base::RefCountedThreadSafe<TestDataGenerator>
{
public:
	virtual void SetUp(MessageLoopForIO *  io_loop)
	{
		data_observer = NULL;
		counter_ = 0;
		data_observer = NULL;
		message_loop = io_loop;
	}

	void  BeginCollectData(  )
	{ 
		message_loop->PostDelayedTask(FROM_HERE, base::Bind(&TestDataGenerator::UpdateData,  this ),
			base::TimeDelta::FromMilliseconds(20)); 
	}

	void  SetObserver( DataAcquisition::IChannelDataObserver*  observer )
	{
		data_observer = observer;
	}
	void  UpdateData()
	{
		using  boost::shared_array;

		const static  int kStatisticDataCount = kUpdateDataCount/ (DataManager::kStatisticsGranularity) + 1;

		shared_array<uint16>	raw_data(new uint16[kUpdateDataCount]);
        for (uint16 i = 0; i < kUpdateDataCount; i++)
        {
			 ++counter_;
            *(raw_data.get()+i) =  static_cast<uint16>(counter_);
        }
	//	raw_data_container.Push(raw_data);
		if(data_observer)
		{
			using namespace DataAcquisition ;
			MessageLoop* io_loop = Content::ThreadManager::GetInstance()->GetThreadMessageLoop(Content::ThreadManager::IO_THREAD);
			io_loop->PostTask(FROM_HERE, 
				base::Bind( base::IgnoreResult(&IChannelDataObserver::OnRawDataUpdate),
				base::Unretained(data_observer) ,raw_data, kUpdateDataCount));
		}
  
		message_loop->PostDelayedTask(FROM_HERE, base::Bind(&TestDataGenerator::UpdateData,  this ),
		base::TimeDelta::FromMilliseconds(20)); 
	}

protected:
	MessageLoopForIO	*		message_loop;
	DataAcquisition::IChannelDataObserver* 	data_observer;

	int64				counter_ ;
 
	const	static int  kDataCount =  0x1<<14 <<2;
};
 